package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.BdFeeRateDao;
import com.fdb.efp.nls.service.domain.BdFeeRate;
import com.fdb.efp.nls.service.facade.BdFeeRateService;
import com.fdb.efp.nls.service.facade.BdFeeRateTempService;
import com.fdb.efp.nls.service.vo.BdFeeRateTempVO;
import com.fdb.efp.nls.service.vo.BdFeeRateVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 费率表
 * <p>
 * 
 * @author ligm
 * @date 2019-03-07
 */
@Service("bdFeeRateService")
public class BdFeeRateServiceImpl implements BdFeeRateService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BdFeeRateServiceImpl.class);

	@Autowired
	private BdFeeRateDao bdFeeRateDao;

	@Autowired
	@Qualifier("bdFeeRateTempService")
	private BdFeeRateTempService bdFeeRateTempService;

	/**
	 * 新增操作
	 */
	public int insertBdFeeRate(BdFeeRateVO inBdFeeRateVo) {
		logger.debug("当前新增数据为:" + inBdFeeRateVo.toString());
		int num = 0;
		try {
			BdFeeRate bdFeeRate = new BdFeeRate();
			beanCopy(inBdFeeRateVo, bdFeeRate);
			num = bdFeeRateDao.insertBdFeeRate(bdFeeRate);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BdFeeRateVO inBdFeeRateVo) {
		logger.debug("当前删除数据条件为:" + inBdFeeRateVo);
		int num = 0;
		try {
			BdFeeRate bdFeeRate = new BdFeeRate();
			beanCopy(inBdFeeRateVo, bdFeeRate);
			num = bdFeeRateDao.deleteByPk(bdFeeRate);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdFeeRateVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(BdFeeRateVO inBdFeeRateVo) {
		logger.debug("当前修改数据为:" + inBdFeeRateVo.toString());
		int num = 0;
		try {
			BdFeeRate bdFeeRate = new BdFeeRate();
			beanCopy(inBdFeeRateVo, bdFeeRate);
			num = bdFeeRateDao.updateByPk(bdFeeRate);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdFeeRateVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BdFeeRateVO queryByPk(BdFeeRateVO inBdFeeRateVo) {

		logger.debug("当前查询参数信息为:" + inBdFeeRateVo);
		try {
			BdFeeRate queryBdFeeRate = new BdFeeRate();
			beanCopy(inBdFeeRateVo, queryBdFeeRate);
			BdFeeRate queryRslBdFeeRate = bdFeeRateDao.queryByPk(queryBdFeeRate);
			if (Objects.nonNull(queryRslBdFeeRate)) {
				BdFeeRateVO outBdFeeRateVo = beanCopy(queryRslBdFeeRate, new BdFeeRateVO());
				logger.debug("当前查询结果为:" + outBdFeeRateVo.toString());
				return outBdFeeRateVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdFeeRateVO> queryAll(BdFeeRateVO bdFeeRateVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BdFeeRateVO> list = null;
		try {
			List<BdFeeRate> bdFeeRates = bdFeeRateDao.queryAllByPage(bdFeeRateVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + bdFeeRates.size());
			pageSet(bdFeeRates, bdFeeRateVo);
			list = (List<BdFeeRateVO>) beansCopy(bdFeeRates, BdFeeRateVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdFeeRateVO> queryAllCurrOrg(BdFeeRateVO bdFeeRateVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BdFeeRate> bdFeeRates = bdFeeRateDao.queryAllCurrOrgByPage(bdFeeRateVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + bdFeeRates.size());
		List<BdFeeRateVO> list = null;
		try {
			pageSet(bdFeeRates, bdFeeRateVo);
			list = (List<BdFeeRateVO>) beansCopy(bdFeeRates, BdFeeRateVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdFeeRateVO> queryAllCurrDownOrg(BdFeeRateVO bdFeeRateVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BdFeeRate> bdFeeRates = bdFeeRateDao.queryAllCurrDownOrgByPage(bdFeeRateVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bdFeeRates.size());
		List<BdFeeRateVO> list = null;
		try {
			pageSet(bdFeeRates, bdFeeRateVo);
			list = (List<BdFeeRateVO>) beansCopy(bdFeeRates, BdFeeRateVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 
	 * 批量新增费率信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchInsertBdFeeRate(List<BdFeeRateTempVO> addList) {
		logger.debug("当前新增数据条数为:" + addList.size());
		List<BdFeeRate> bdFeeRateList = new ArrayList<BdFeeRate>();
		int num = 0;
		try {
			num = bdFeeRateDao.batchInsertBdFeeRate(bdFeeRateList);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 
	 * 批量更新费率信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchUpdateBdFeeRate(List<BdFeeRateTempVO> updateList) {
		logger.debug("当前修改数据为:" + updateList.size() + "条");
		List<BdFeeRate> bdFeeRateList = new ArrayList<BdFeeRate>();
		int num = 0;
		try {
			num = bdFeeRateDao.batchUpdateBdFeeRate(bdFeeRateList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	/**
	 * 
	 * 同步托管机构资金划扣临时表数据到托管机构资金划扣正式表有数据则更新，无则新增 <br>
	 * 0.1:ludayong:2019年3月23日 下午12:09:28:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月23日 下午12:09:28
	 * @date 2019年3月23日 下午12:09:28
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateBdFeeRate(List<BdFeeRateVO> list) {
		LocalDate today = LocalDate.now();
		String msg1 = "【手工减免息费明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<BdFeeRate> bdFeeRatelList = new ArrayList<BdFeeRate>();
		int num = 0;
		try {
			bdFeeRatelList = (List<BdFeeRate>) beansCopy(list, BdFeeRate.class);
			num = bdFeeRateDao.insertOrUpdateBdFeeRate(bdFeeRatelList);
			String num_msg = "【手工减免息费明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【手工减免息费明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
}
